package com.vgooo.client.tool.tester;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.widget.ListView;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.vgooo.client.R;
import com.vgooo.util.UmengUtils;

public class TestCommun extends BaseTestActivity {

	final static String KEY = "commun";

	ListView listview;

	TextView imeiValue;
	TextView wifiValue;
	TextView bluetoochValue;

	public boolean isEnabledWifi = false;
	public boolean isEnabledBluetooch = false;

	WifiManager wifiManager;
	BluetoothAdapter bluetoothAdapter;

	Thread wifiThread;
	Thread bluetoochThread;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		init(KEY);

		setContentView(R.layout.test_commun);

		imeiValue = (TextView) findViewById(R.id.imei_value);
		wifiValue = (TextView) findViewById(R.id.wifi_value);
		bluetoochValue = (TextView) findViewById(R.id.bluetooch_value);

		TelephonyManager tm = (TelephonyManager) this
				.getSystemService(Context.TELEPHONY_SERVICE);
		imeiValue.setText(tm.getDeviceId());

		// wifi
		wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

		isEnabledWifi = wifiManager.isWifiEnabled();

		wifiManager.setWifiEnabled(true);

		// bluetooch
		bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

		isEnabledBluetooch = bluetoothAdapter.isEnabled();

		bluetoothAdapter.enable();

		wifiThread = new Thread(new Runnable() {

			@Override
			public void run() {
				while (!wifiManager.isWifiEnabled()) {
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						UmengUtils.onError(TestCommun.this, e.toString());
					}
				}

				wifiHandler.sendMessage(new Message());
			}
		});

		bluetoochThread = new Thread(new Runnable() {

			@Override
			public void run() {
				while (!bluetoothAdapter.isEnabled()) {
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				bluetoochHandler.sendMessage(new Message());
			}
		});

		wifiThread.start();
		bluetoochThread.start();
	}

	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		// 友盟统计功能，打开页面
		MobclickAgent.onResume(this);
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		// 友盟统计功能，页面关闭
		MobclickAgent.onPause(this);
		super.onPause();
	}
	
	Handler wifiHandler = new Handler() {
		public void handleMessage(Message msg) {
			wifiValue.setText(wifiManager.getConnectionInfo().getMacAddress());
			super.handleMessage(msg);
		}
	};
	Handler bluetoochHandler = new Handler() {
		public void handleMessage(Message msg) {
			bluetoochValue.setText(bluetoothAdapter.getAddress());
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

		WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		wifiManager.setWifiEnabled(isEnabledWifi);

		BluetoothAdapter bluetoothAdapter = BluetoothAdapter
				.getDefaultAdapter();

		if (isEnabledBluetooch)
			bluetoothAdapter.enable();
		else
			bluetoothAdapter.disable();
	}
}
